package com.ems.service.impl.Invigilate.room;

import com.ems.dao.Invigilate.room.RoomPaperDao;
import com.ems.dao.Invigilate.room.RoomUserDao;
import com.ems.dao.Invigilate.room.TestRoomDao;
import com.ems.pojo.Invigilate.room.RoomPaper;
import com.ems.pojo.Invigilate.room.RoomUser;
import com.ems.pojo.Invigilate.room.TestRoom;
import com.ems.pojo.user.User;
import com.ems.service.Invigilate.room.RoomPaperService;
import com.ems.service.Invigilate.room.RoomUserService;
import com.ems.service.Invigilate.room.TestRoomService;
import com.ems.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Auther: 伟伟
 * @Date: 2020/2/18
 * @Description: 描述
 * @Version: 1.0
 */
@Service("testRoomService")
public class TestRoomServiceImpl implements TestRoomService {
    @Autowired
    private TestRoomDao testRoomDao;

    @Autowired
    private RoomUserDao roomUserDao;

    @Autowired
    private RoomPaperDao roomPaperDao;

    @Autowired
    @Qualifier("roomUserService")
    private RoomUserService roomUserService;

    @Autowired
    @Qualifier("roomPaperService")
    private RoomPaperService roomPaperService;

    @Override
    public void addTestRoom(TestRoom testRoom) {
         testRoom.setTr_time(Tools.getTime());
         testRoomDao.addTestRoom(testRoom);
    }

    @Override
    public void deleteTestRoom(List<TestRoom> list) {
        if(list != null && list.size() > 0){
            //在room_user表中删除考场中的考生



            //删除考场
            for(int i = 0;i < list.size();i ++){
                testRoomDao.deleteTestRoom(list.get(i).getTr_id());
            }

        }

    }

    @Override
    public void updateTestRoom(TestRoom testRoom) {
         testRoomDao.updateTestRoom(testRoom);
    }

    @Override
    public List<TestRoom> selectTestRoom(Map<String, Object> map,User user) {
        List<TestRoom> list = null;
        if(map.get("tr_name") != null && map.get("tr_name").toString().trim().length() != 0){
            list = testRoomDao.selectTestRoom("%"+map.get("tr_name")+"%",user.getId());
        }else{
            list = testRoomDao.selectTestRoom(null,user.getId());
        }

        if(list.size() > 0){
            for(int i = 0;i < list.size();i ++){
                TestRoom testRoom = list.get(i);

                //获取考场中的人数
                List<RoomUser> roomUserList = roomUserDao.getRoomUserByTrId(testRoom.getTr_id());
                testRoom.setUserNum(roomUserList.size());
                //获取考场中的考题数
                List<RoomPaper> roomPaperList =roomPaperDao.getRoomPaperByTrId(testRoom.getTr_id());
                testRoom.setRoomPaperNum(roomPaperList.size());
            }
        }
        return list;
    }

    @Override
    public List<TestRoom> selectUnRoomUser(Map<String, Object> map, User user) {
        //获取监考者下面所有有申请的考场信息
        List<TestRoom> list = null;
        if(map.get("tr_name") != null && map.get("tr_name").toString().trim().length() != 0){
            list = testRoomDao.selectUnTestRoom("%"+map.get("tr_name")+"%",user.getId());
        }else{
            list = testRoomDao.selectUnTestRoom(null,user.getId());
        }
        if(list.size() > 0){
            //查询该考场下的所有申请信息
            TestRoom testRoom = null;
            for(int i = 0;i < list.size();i ++){
                testRoom = list.get(i);
                List<RoomUser> roomUserList = roomUserService.getUnRoomUserByTrId(testRoom.getTr_id());
                testRoom.setRoomUserList(roomUserList);
            }
        }

        //排除

        return list;
    }

    //查询监考下面的所有考场信息
    @Override
    public List<TestRoom> selectAllTestRoomById(User user) {
        return testRoomDao.selectAllTestRoomById(user);
    }

    //根据id查询指定的考场信息
    @Override
    public TestRoom selectTestRoomById(TestRoom testRoom) {
        TestRoom testRoom1 = testRoomDao.selectTestRoomById(testRoom);
        //获取考场中的人数
        List<RoomUser> roomUserList = roomUserDao.getRoomUserByTrId(testRoom1.getTr_id());
        testRoom1.setUserNum(roomUserList.size());
        //获取考场中的考题数
        List<RoomPaper> roomPaperList =roomPaperDao.getRoomPaperByTrId(testRoom1.getTr_id());
        testRoom1.setRoomPaperNum(roomPaperList.size());
        return testRoom1;
    }

    @Override
    public TestRoom getTestRoomByPwd(TestRoom testRoom) {
        return testRoomDao.getTestRoomByPwd(testRoom);
    }

    @Override
    public void deleteTestRoolById(User user) {
        //查询该用户所创建考场的信息
        List<TestRoom> testRoomList = testRoomDao.queryTestRoomById(user.getId());
        if(testRoomList != null && testRoomList.size() > 0){
            TestRoom testRoom = null;
            for(int i = 0;i < testRoomList.size();i ++){
                testRoom = testRoomList.get(i);
                //从room_user表中删除信息
                roomUserService.deleteRoomUserByTrId(testRoom.getTr_id());

                //从room_paper表中删除信息
                roomPaperService.deleteRoomPaperByTrId(testRoom.getTr_id());

                //删除test_room表中的信息
                testRoomDao.deleteTestRoom(testRoom.getTr_id());
            }
        }

    }
}
